/**
* ...
* @author Ludo
* @version 1.1
*/

import clases.MadParser.IndividualStats.MadEquipo;
import clases.MadParser.TeamStats.MadJugador;
import clases.MadParser.MadParser;
import mx.utils.Delegate;
import flash.external.ExternalInterface;
import clases.utils.MadGrafica;

class clases.MadTeamStats extends MovieClip {

	// Modo local
	public static var local:Boolean = false;

	// Ruta
	public static var pathLocal:String = "xml/Tercero.xml";

	// Fichero XML
	private var xmlFile:XML;
	
	// Parseador
	private var madParser:MadParser;

	
	// Textos
	
	public var team1:TextField;
	public var team2:TextField;
	
	
	// Textos de cabecera
	private var name: String;
	private var timePlayed:String;
	private var totalPoints:String;
	private var twoTotalPoints:String;
	private var twoPercent:String;
	private var threeTotalPoints:String;
	private var threePercent:String;
	private var freeThrowTotalPoints:String;
	private var freeThrowPercent:String;
	private var offensiveRebounds:String;
	private var defensiveRebounds:String;
	private var totalRebounds:String;
	private var assistances:String;
	private var steels:String;
	private var turnOvers:String;
	private var blocksFavor:String;
	private var blocksAgainst:String;
	private var faultsCommitted:String;
	private var faultsReceived:String;
	private var ranking:String;
	
	// Cajas de texto
	
	private var txLiteralFaltasRecibidas:TextField;
	private var txLiteralFaltasCometidas:TextField;
	private var txLiteralTaponesEnContra:TextField;
	private var txLiteralTaponesAFavor:TextField;
	private var txLiteralTirosLibres:TextField;
	private var txLiteralRobos:TextField;
	private var txLiteralPerdidas:TextField;
	private var txLiteralAsistencias:TextField;
	private var txLiteralRebotes:TextField;
	private var txLiteralCanastaTres:TextField;
	private var txLiteralCanastaDos:TextField;
	private var txLiteralPuntos:TextField;
	private var txLiteralMinutosJugados:TextField;
	private var txLiteralFaltasRecibidas2:TextField;
	private var txLiteralFaltasCometidas2:TextField;
	private var txLiteralTaponesEnContra2:TextField;
	private var txLiteralTaponesAFavor2:TextField;
	private var txLiteralTirosLibres2:TextField;
	private var txLiteralRobos2:TextField;
	private var txLiteralPerdidas2:TextField;
	private var txLiteralAsistencias2:TextField;
	private var txLiteralRebotes2:TextField;
	private var txLiteralCanastaTres2:TextField;
	private var txLiteralCanastaDos2:TextField;
	private var txLiteralPuntos2:TextField;
	private var txLiteralMinutosJugados2:TextField;
	private var txFaltasRecibidas:TextField;
	private var txFaltasCometidas:TextField;
	private var txTaponesEnContra:TextField;
	private var txTaponesAFavor:TextField;
	private var txTirosLibres:TextField;
	private var txRobos:TextField;
	private var txPerdidas:TextField;
	private var txAsistencias:TextField;
	private var txRebotes:TextField;
	private var txCanastaTres:TextField;
	private var txCanastaDos:TextField;
	private var txPuntos:TextField;
	private var txMinutosJugados:TextField;
	private var txFaltasRecibidas2:TextField;
	private var txFaltasCometidas2:TextField;
	private var txTaponesEnContra2:TextField;
	private var txTaponesAFavor2:TextField;
	private var txTirosLibres2:TextField;
	private var txRobos2:TextField;
	private var txPerdidas2:TextField;
	private var txAsistencias2:TextField;
	private var txRebotes2:TextField;
	private var txCanastaTres2:TextField;
	private var txCanastaDos2:TextField;
	private var txPuntos2:TextField;
	private var txMinutosJugados2:TextField;	
	private var txEquipo1:TextField;
	private var txEquipo2:TextField;	
	
	
	// Graficas
	public var mcTwoPointsPercent:MovieClip;
	public var mcThreePointsPercent:MovieClip;
	public var mcFreeThrowPercent:MovieClip;
	public var mcTotalRebounds:MovieClip;
	public var mcAssistances:MovieClip;
	public var mcSteels:MovieClip;
	public var mcTurnOvers:MovieClip;
	public var heightStandard:Number;
	
	
	public function MadTeamStats() 
	{
		this._visible = false;
		ExternalInterface.addCallback("pasaXMLTeamStatsAFlash", null, Delegate.create(this, onLoadExternalXML));
		ExternalInterface.addCallback("preparaCarga", null, Delegate.create(this,preparaCarga));
		ExternalInterface.addCallback("errorProducido", null, Delegate.create(this,errorCargaExterna));
		
		
		if (_level0.Lenguaje == "he")
		{
			gotoAndStop(2);
		}

		preparaLiterales();
		
		
		heightStandard = new Number(mcTurnOvers.mcTeam1._height);
		

		preparaCarga();
		
		madParser = new MadParser();
		if(local)
		{
			getLocalXML();
		}
		else
		{
			trace("Hago la llamada a cargaTeamStats");
			ExternalInterface.call("cargaTeamStats");
		}
		
		Stage.scaleMode = "noScale";
		Stage.align = "TL";
		Stage.addListener(this);
	}
	
	private function onResize()
	{
				
	}
	
	private function preparaLiterales()
	{
		name = _level0.Nombre;		
		/*timePlayed = _level0.Minutos;
		totalPoints = _level0.Pts;
		twoTotalPoints = _level0["2Pt"];
		twoPercent = _level0["2Pt"];
		threeTotalPoints = _level0["3Pt"];
		threePercent = _level0["3Pt"];
		freeThrowTotalPoints = _level0.TL;
		freeThrowPercent = _level0.TL;
		offensiveRebounds = _level0.O;
		defensiveRebounds = _level0.D;
		totalRebounds= _level0.T;
		assistances = _level0.As;
		steels = _level0.Ro;
		turnOvers = _level0.P;
		blocksFavor = _level0.Tapones + " " + _level0.Afavor;
		blocksAgainst = _level0.Tapones + " " + _level0.EnContra;
		faultsCommitted = _level0.Faltas + " " + _level0.Cometidas;
		faultsReceived = _level0.Faltas + " " + _level0.Recibidas;
		ranking = _level0.Val;			*/
		
		txLiteralMinutosJugados.text = _level0.Minutos;
		txLiteralPuntos.text = _level0.Pts;
		txLiteralFaltasRecibidas.text = _level0.Faltas + " " + _level0.Recibidas;
		txLiteralFaltasCometidas.text = _level0.Faltas + " " + _level0.Cometidas;
		txLiteralTaponesEnContra.text = _level0.Tapones + " " + _level0.EnContra;
		txLiteralTaponesAFavor.text = _level0.Tapones + " " + _level0.Afavor;
		txLiteralTirosLibres.text = _level0.TirosLibres;
		txLiteralRobos.text = _level0.Robos;
		txLiteralPerdidas.text = _level0.Perdidas;
		txLiteralAsistencias.text = _level0.Asistencias;
		txLiteralRebotes.text = _level0.TotalRebotes;
		txLiteralCanastaTres.text = _level0.CanastaDos;
		txLiteralCanastaDos.text = 	_level0.CanastaTresPuntos;
		
		txLiteralMinutosJugados2.text = _level0.Minutos;
		txLiteralPuntos2.text = _level0.Pts;
		txLiteralFaltasRecibidas2.text = _level0.Faltas + " " + _level0.Recibidas;
		txLiteralFaltasCometidas2.text = _level0.Faltas + " " + _level0.Cometidas;
		txLiteralTaponesEnContra2.text = _level0.Tapones + " " + _level0.EnContra;
		txLiteralTaponesAFavor2.text = _level0.Tapones + " " + _level0.Afavor;
		txLiteralTirosLibres2.text = _level0.TirosLibres;
		txLiteralRobos2.text = _level0.Robos;
		txLiteralPerdidas2.text = _level0.Perdidas;
		txLiteralAsistencias2.text = _level0.Asistencias;
		txLiteralRebotes2.text = _level0.TotalRebotes;
		txLiteralCanastaTres2.text = _level0.CanastaDos;
		txLiteralCanastaDos2.text = _level0.CanastaTresPuntos;
		
		if (_level0.Lenguaje == undefined)
		{
			_level0.Lenguaje = "he";
		}		
		
		
	}
	private function getLocalXML()
	{
		xmlFile = new XML();
		xmlFile.ignoreWhite = true;
		xmlFile.onLoad = Delegate.create(this,onLoadXML);
/*		xmlFile.onLoad = function(ok)
		{
			trace(this);
		}*/
		xmlFile.load(pathLocal);
	}
	
	private function onLoadXML(ok:Boolean)
	{
		if(ok)
		{
			changeTeamStats(xmlFile.toString());
		}
		else
		{
			quitaCarga();
			trace("** ERROR ** Carga de " + pathLocal);
		}
	}
	
	private function onLoadExternalXML(data:String)
	{
		trace("Llego al cambio de individual stats con " + this);
		changeTeamStats(data);
		
	}	

	
	private function changeTeamStats(data:String):Boolean
	{
		if (!madParser.parseTeamStats(data))
		{
			errorParseado();
			return false;
		}
		
		if(!local)
		{
			ExternalInterface.call("cargaTeamStats",madParser.expires*1000);
		}
		else
		{
			var intervalo = setInterval(this,"changeTeamStats",5000,xmlFile.toString());
		}
		
		var arrayPlayers:Array = new Array();
		
		txEquipo1.text = madParser.team1;
		txEquipo2.text = madParser.team2;
		var team1:Object = madParser.team1Info;
		txFaltasRecibidas.text = team1.faultsReceived;
		txFaltasCometidas.text = team1.faultsCommitted;
		txTaponesEnContra.text = team1.blocksAgainst;
		txTaponesAFavor.text = team1.blocksFavor;
		txTirosLibres.text = team1.freeThrowTotals;
		txPerdidas.text = team1.turnOvers;
		txRobos.text = team1.steels;
		txAsistencias.text = team1.assistances;
		txRebotes.text = team1.totalRebounds;
		txCanastaTres.text = team1.threeSuccess;
		txCanastaDos.text = team1.twoSuccess;
		txPuntos.text = team1.totalPoints;
		txMinutosJugados.text =	madParser.minutesPlayed;
		
		
		var team2:Object = madParser.team2Info;
		txFaltasRecibidas2.text = team2.faultsReceived;
		txFaltasCometidas2.text = team2.faultsCommitted;
		txTaponesEnContra2.text = team2.blocksAgainst;
		txTaponesAFavor2.text = team2.blocksFavor;
		txTirosLibres2.text = team2.freeThrowTotals;
		txPerdidas2.text = team2.turnOvers;
		txRobos2.text = team2.steels;
		txAsistencias2.text = team2.assistances;
		txRebotes2.text = team2.totalRebounds;
		txCanastaTres2.text = team2.threeSuccess;
		txCanastaDos2.text = team2.twoSuccess;
		txPuntos2.text = team2.totalPoints;
		txMinutosJugados2.text =	madParser.minutesPlayed;

		makeGraph();
		this._visible = true;
		quitaCarga();
		return true;

	}
	
	private function makeGraph()
	{
		var team1:Object = madParser.team1Info;
		var team2:Object = madParser.team2Info;
		
		mcTwoPointsPercent.txVal1.text = team1.twoPercent;
		mcTwoPointsPercent.txVal2.text = team2.twoPercent;
		mcTwoPointsPercent.txTitle.text = _level0.DosPuntosPorCiento.split("porciento").join("%");
		trace(_level0.DosPuntosPorCiento);

		
		
		mcTwoPointsPercent.mcTeam1._height = team1.twoPercent * heightStandard / 100;
		mcTwoPointsPercent.mcTeam2._height = team2.twoPercent * heightStandard / 100;

		mcThreePointsPercent.txVal1.text = team1.threePercent;
		mcThreePointsPercent.txVal2.text = team2.threePercent;
		mcThreePointsPercent.txTitle.text = _level0.TresPuntosPorCiento.split("porciento").join("%");
		trace(_level0.TresPuntosPorCiento);
		mcThreePointsPercent.mcTeam1._height = team1.threePercent * heightStandard/ 100;
		mcThreePointsPercent.mcTeam2._height = team2.threePercent * heightStandard / 100;
		
		mcFreeThrowPercent.txVal1.text = team1.freeThrowPercent;
		mcFreeThrowPercent.txVal2.text = team2.freeThrowPercent;
		mcFreeThrowPercent.txTitle.text = _level0.TirosLibres;
		mcFreeThrowPercent.mcTeam1._height = team1.freeThrowPercent  * heightStandard / 100;
		mcFreeThrowPercent.mcTeam2._height = team2.freeThrowPercent  * heightStandard/ 100;		

		mcTotalRebounds.txVal1.text = team1.totalRebounds;
		mcTotalRebounds.txVal2.text = team2.totalRebounds;
		mcTotalRebounds.txTitle.text = _level0.TotalRebotes;
		var totalRebounds:Number = new Number(team1.totalRebounds + team2.totalRebounds);
		mcTotalRebounds.mcTeam1._height = team1.totalRebounds * heightStandard / totalRebounds;
		mcTotalRebounds.mcTeam2._height = team2.totalRebounds * heightStandard / totalRebounds;		

		mcAssistances.txVal1.text = team1.assistances;
		mcAssistances.txVal2.text = team2.assistances;
		mcAssistances.txTitle.text = _level0.Asistencias;		
		var totalAssistances:Number = new Number(team1.assistances + team2.assistances);
		mcAssistances.mcTeam1._height = team1.assistances * heightStandard / totalAssistances;
		mcAssistances.mcTeam2._height = team2.assistances * heightStandard / totalAssistances;		

		mcSteels.txVal1.text = team1.steels;
		mcSteels.txVal2.text = team2.steels;
		mcSteels.txTitle.text = _level0.Robos;				
		var totalSteels:Number = new Number(team1.steels + team2.steels);
		mcSteels.mcTeam1._height = team1.steels * heightStandard / totalSteels;
		mcSteels.mcTeam2._height = team2.steels * heightStandard / totalSteels;		
		
		mcTurnOvers.txVal1.text = team1.turnOvers;
		mcTurnOvers.txVal2.text = team2.turnOvers;
		mcTurnOvers.txTitle.text = _level0.Robos;
		var totalTurnOvers:Number = new Number(team1.turnOvers + team2.turnOvers);
		trace(mcTurnOvers.mcTeam1._height);
		mcTurnOvers.mcTeam1._height = team1.turnOvers * heightStandard / totalTurnOvers;
		mcTurnOvers.mcTeam2._height = team2.turnOvers * heightStandard / totalTurnOvers;		
		
	}

	private function preparaCarga()
	{
		_level0.mcLoading._visible = true;
		_level0.mcLoading.gotoAnPlay(1);
	}

	private function quitaCarga()
	{
		_level0.mcLoading._visible = false;
		_level0.mcLoading.gotoAnStop(1);
	}	

	private function errorCargaExterna()
	{
		ExternalInterface.call("cargaTeamStats",1000);		
	}

	private function errorParseado()
	{
		ExternalInterface.call("cargaTeamStats",1000);		
	}	
	
}